/**
 * Created by root on 15-1-3.
 */

define(['lessonJS/module'], function(lessonModule){

  lessonModule.factory('LessonService', ['ResourceService', function(ResourceService){
    return {
      accessor: ResourceService.getResourceAccessor({ resourceName: 'lesson' }),

      getLessonList: function(){
        var self = this;

        return self.accessor.get();
      },
      showSpecifyLesson: function(lessonId){
        var self = this;

        return self.accessor.show(lessonId);
      },
      getTempCoverUri: function(){
        return 'api/tmp-file/image';
      }
    }
  }]);


  lessonModule.factory('LessonCallnameService', ['ResourceService', function(ResourceService){

    return {
      accessor: ResourceService.getResourceAccessor({
        resourceName: 'callname'
      }),
      getExcelUploadUri: function(){
        var self = this;

        return self.accessor.getBaseUrl() + '/excel';
      }
    };
  }]);

  lessonModule.factory('LessonCallnameStudentsService', ['NestedResourceService',
    function(NestedResourceService){

      return {
        accessor: NestedResourceService.getResourceAccessor({
          parentResourceName: 'callname',
          nestedResourceName: 'student'
        }),
        addStudent: function (student) {
          var self = this;

          return self.accessor.store(student);
        },
        destroyMany: function(students){
          var self = this;

          var ids = [];
          students.forEach(function(student){
            ids.push(student.id);
          });

          return self.accessor.destroy(ids.join('-'));
        }
      };
  }]);

  /**
   * 实现点名列表操作的服务
   */
  lessonModule.factory('LessonCallnameDoService', [
    function(){

      var filterMethods = {
        all: {
          label: '对所有学生进行点名',
          filter: function(){
            return true;
          }
        },
        absent: {
          label: '对有缺勤记录的学生进行点名',
          filter: function(student){
            if( student['absent_times'] > 0 ){
              return true;
            }
            else{
              return false;
            }
          }
        },
        attend: {
          label: '对没有缺勤记录的学生进行点名',
          filter: function(student){
            if( student['absent_times'] == 0 ){
              return true;
            }
            else{
              return false;
            }
          }
        }
      };

      return {
        getFilterConditions: function(){
          var rtn = [];
          for(var method in filterMethods ){
            rtn.push({
              label: filterMethods[ method ][ 'label' ],
              value: method
            })
          }

          return rtn;
        },
        getFilterMethodLabel: function(methodName){
          return filterMethods[methodName]['label'];
        },
        filterStudents: function(students, filterName){
          var rtn = [];

          students.forEach(function(student){
            if( filterMethods[filterName].filter(student) ){
              rtn.push(student);
            }
          });

          return rtn;
        }
      };
    }
  ]);//End of --> LessonCallnameDoService

  lessonModule.factory('LessonStudentService', ['NestedResourceService', function(NestedResourceService){
    return {
      accessor: NestedResourceService.getResourceAccessor({
        parentResourceName: 'lesson',
        nestedResourceName: 'student'}),

      init: function(lessonId){
        var self = this;

        self.accessor.setParentResourceId(lessonId);

        return self.accessor;
      }
    }
  }]);

  /**
   * 用于操作课程资源的服务.
   */
  lessonModule.factory('LessonResourceService', ['$window', 'NestedResourceService', 'LoginStatusService',
    function($window, NestedResourceService, LoginStatusService){
      var downloadAccessor = null;

      //过滤方法的集合
      var filterMethods = {
        "from_teacher": {
          "condition": [
            {label: '所有人分享的', cond: 'all'},
            {label:"老师分享的", cond: 'teacher'},
            {label:"学生分享的", cond: 'student'}
          ],
          "method": function(source, cond){
            if( cond == 'all' ){
              return source;
            }

            source = source || [];

            var rtn = [];
            source.forEach(function(curr){
              if( curr.from_teacher && cond == 'teacher' ){
                rtn.push(curr);
              }

              if( ! curr.from_teacher && cond == 'student'){
                rtn.push(curr);
              }
            });

            return rtn;
          }
        },
        "on_user": {
          "condition": [
            {label:"所有人的", cond:"all"},
            {label:"我上传的", cond:"mine"}
          ],
          "method": function(source, cond){
            if( cond == 'all' ){
              return source;
            }

            var currUserId = LoginStatusService.get('personalInfo.id'),
              rtn = [];
            source = source || [];

            source.forEach(function(curr){
              if( curr.creater_id == currUserId ){
                rtn.push(curr);
              }
            });

            return rtn;
          }
        }
      };

      return{
        accessor: NestedResourceService.getResourceAccessor({
          parentResourceName: 'lesson',
          nestedResourceName: 'resource'
        }),
        getFilterConditions: function(){
          var rtn = {};
          for(var item in filterMethods ){
            rtn[item] = filterMethods[item]['condition'];
          }

          return rtn;
        },
        filterOnCondition: function(source, condObj){
          var rtn = source;
          for(var item in condObj){
            rtn = filterMethods[item].method(rtn, condObj[item]);
          }

          return rtn;
        },
        getUploadUri: function(stateParams){
          var self = this;

          if( stateParams ){
            self.accessor['setParentResourceId']($stateParams);
          }

          return self.accessor.getBaseUrl();
        },
        downloadResource: function(resourceId){
          if( downloadAccessor === null ){
            downloadAccessor = NestedResourceService.getResourceAccessor({
              parentResourceName: 'lesson',
              nestedResourceName: 'resource',
              apiPrefix: 'download'
            });
          }

          $window.location = downloadAccessor.getBaseUrl() + '/' + resourceId;
        }
      };
  }]);//End of --> LessonResourceService

});